package org.swim.core.entities;

// Generated Dec 7, 2012 12:57:21 AM by Hibernate Tools 3.4.0.CR1

import static javax.persistence.GenerationType.IDENTITY;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.swim.remote.entities.IAbility;
import org.swim.remote.entities.ICollaboration;
import org.swim.remote.entities.IFeedback;
import org.swim.remote.entities.IFriendship;
import org.swim.remote.entities.IMessage;
import org.swim.remote.entities.IUser;

/**
 * User generated by hbm2java
 */

@Entity
@Table(name = "User", catalog = "jboss", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class User extends SwimEntity implements java.io.Serializable, IUser {

	private static final long serialVersionUID = 1L;

	private Integer id;
	private String name = Default.NAME.toString();
	private String surname = Default.SURNAME.toString();
	private String email;
	private String password = Default.PASSWORD.toString();
	private Status status = Default.STATUS.toStatus();
	private Date birth;
	private byte[] photo;
	private String permissions;
	private Set<Feedback> feedbacksForReceiverId = new HashSet<Feedback>(0);
	private Set<Collaboration> collaborationsForReceiverId = new HashSet<Collaboration>(
			0);
	private Set<Collaboration> collaborationsForSenderId = new HashSet<Collaboration>(
			0);
	private Set<Friendship> friendshipsForReceiverId = new HashSet<Friendship>(
			0);
	private Set<Message> messagesForSenderId = new HashSet<Message>(0);
	private Set<Ability> abilities = new HashSet<Ability>(0);
	private Set<Message> messagesForReceiverId = new HashSet<Message>(0);
	private Set<Feedback> feedbacksForSenderId = new HashSet<Feedback>(0);
	private Set<Friendship> friendshipsForSenderId = new HashSet<Friendship>(0);

	private FeedbackAvg feedbackAvg;

	public User() {
	}

	public User(String name, String surname, String email, String password,
			Status status) {
		this.name = name;
		this.surname = surname;
		this.email = email;
		this.password = password;
		this.status = status;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "name", nullable = false, length = 45)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Transient
	public String getFullName() {
		String fullName = "";

		if (!name.toUpperCase().equals(Default.NAME.toString().toUpperCase())) {
			fullName += name + " ";
		}
		if (!surname.toUpperCase().equals(
				Default.SURNAME.toString().toUpperCase())) {
			fullName += surname;
		}

		if (fullName.equals("")) {
			fullName = "NO NAME AND NO SURNAME";
		}

		return fullName;
	}

	@Column(name = "surname", nullable = false, length = 45)
	public String getSurname() {
		return this.surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	@Column(name = "email", unique = true, nullable = false, length = 45)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "password", nullable = false, length = 45)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "status", nullable = false, length = 12, columnDefinition = "ENUM('UNREGISTERED', 'REGISTERED'")
	public Status getStatus() {
		return this.status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "birth", length = 10)
	public Date getBirth() {
		return this.birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	@Column(name = "photo")
	public byte[] getPhoto() {
		return this.photo;
	}

	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}

	@Column(name = "permissions", length = 65535)
	public String getPermissions() {
		return this.permissions;
	}

	public void setPermissions(String permissions) {
		this.permissions = permissions;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "userByReceiverId")
	public Set<Feedback> getFeedbacksForReceiverId() {
		return this.feedbacksForReceiverId;
	}

	public void setFeedbacksForReceiverId(Set<Feedback> feedbacksForReceiverId) {
		this.feedbacksForReceiverId = feedbacksForReceiverId;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "userByReceiverId")
	public Set<Collaboration> getCollaborationsForReceiverId() {
		return this.collaborationsForReceiverId;
	}

	public void setCollaborationsForReceiverId(
			Set<Collaboration> collaborationsForReceiverId) {
		this.collaborationsForReceiverId = collaborationsForReceiverId;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "userBySenderId")
	public Set<Collaboration> getCollaborationsForSenderId() {
		return this.collaborationsForSenderId;
	}

	public void setCollaborationsForSenderId(
			Set<Collaboration> collaborationsForSenderId) {
		this.collaborationsForSenderId = collaborationsForSenderId;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "userByReceiverId")
	public Set<Friendship> getFriendshipsForReceiverId() {
		return this.friendshipsForReceiverId;
	}

	public void setFriendshipsForReceiverId(
			Set<Friendship> friendshipsForReceiverId) {
		this.friendshipsForReceiverId = friendshipsForReceiverId;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "userBySenderId")
	public Set<Message> getMessagesForSenderId() {
		return this.messagesForSenderId;
	}

	public void setMessagesForSenderId(Set<Message> messagesForSenderId) {
		this.messagesForSenderId = messagesForSenderId;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@OrderBy("name ASC")
	@JoinTable(name = "AbilityUser", catalog = "jboss", joinColumns = { @JoinColumn(name = "userId", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "abilityId", nullable = false, updatable = false) })
	public Set<Ability> getAbilities() {
		return this.abilities;
	}

	public void setAbilities(Set<Ability> abilities) {
		this.abilities = abilities;
	}

	public void setAnAbility(Ability ability) {
		abilities.add(ability);
	}

	public void removeAnAbility(Ability ability) {
		abilities.remove(ability);
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "userByReceiverId")
	public Set<Message> getMessagesForReceiverId() {
		return this.messagesForReceiverId;
	}

	public void setMessagesForReceiverId(Set<Message> messagesForReceiverId) {
		this.messagesForReceiverId = messagesForReceiverId;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "userBySenderId")
	public Set<Feedback> getFeedbacksForSenderId() {
		return this.feedbacksForSenderId;
	}

	public void setFeedbacksForSenderId(Set<Feedback> feedbacksForSenderId) {
		this.feedbacksForSenderId = feedbacksForSenderId;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "userBySenderId")
	public Set<Friendship> getFriendshipsForSenderId() {
		return this.friendshipsForSenderId;
	}

	public void setFriendshipsForSenderId(Set<Friendship> friendshipsForSenderId) {
		this.friendshipsForSenderId = friendshipsForSenderId;
	}

	@OneToOne
	@JoinColumn(name = "id")
	public FeedbackAvg getFeedbackEntity() {
		return feedbackAvg;
	}

	public void setFeedbackEntity(FeedbackAvg feedbackAvg) {
		this.feedbackAvg = feedbackAvg;
	}

	/* --------------------All method for interfaces------------------------ */

	@Transient
	public Double getFeedbackAvgVal() {
		FeedbackAvg feedbackAvg = getFeedbackEntity();
		if (feedbackAvg == null)
			return 0.0;

		Double d = getFeedbackEntity().getFeedbackAvg();
		Integer i = (int) Math.round(d * 10);
		return ((double) i) / 10d;

	}

	@Transient
	public Set<IFeedback> getFeedbacksForReceiverIdSet() {
		Set<IFeedback> set = new HashSet<IFeedback>(getFeedbacksForReceiverId());
		return set;
	}

	@Transient
	public Set<ICollaboration> getCollaborationsForReceiverIdSet() {
		Set<ICollaboration> set = new HashSet<ICollaboration>(
				getCollaborationsForReceiverId());
		return set;
	}

	@Transient
	public Set<ICollaboration> getCollaborationsForSenderIdSet() {
		Set<ICollaboration> set = new HashSet<ICollaboration>(
				getCollaborationsForSenderId());
		return set;
	}

	@Transient
	public Set<IFriendship> getFriendshipsForReceiverIdSet() {
		Set<IFriendship> set = new HashSet<IFriendship>(
				getFriendshipsForReceiverId());
		return set;
	}

	@Transient
	public Set<IMessage> getMessagesForSenderIdSet() {
		Set<IMessage> set = new HashSet<IMessage>(getMessagesForSenderId());
		return set;
	}

	@Transient
	public Set<IAbility> getAbilitiesSet() {
		Set<IAbility> set = new HashSet<IAbility>(getAbilities());
		return set;
	}

	@Transient
	public Set<IMessage> getMessagesForReceiverIdSet() {
		Set<IMessage> set = new HashSet<IMessage>(getMessagesForReceiverId());
		return set;
	}

	@Transient
	public Set<IFeedback> getFeedbacksForSenderIdSet() {
		Set<IFeedback> set = new HashSet<IFeedback>(getFeedbacksForSenderId());
		return set;
	}

	@Transient
	public Set<IFriendship> getFriendshipsForSenderIdSet() {
		Set<IFriendship> set = new HashSet<IFriendship>(
				getFriendshipsForSenderId());
		return set;
	}

}
